﻿using MediatR;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using W.Common;
using W.Common.Enum;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.AllocationCommand;
using W.Warehouse.Read.Api.Application.Command.MoveCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.MoveCommandHander
{
    public class MoveShowCommandHandler : IRequestHandler<MoveShowCommand, ApiResult<MovepageDTO>>
    {
        private readonly IMoveProductRepository moveProductRepository;
        private readonly IMoveTheLibraryRepository moveTheLibraryRepository;
        private readonly IBinLocationRepository _binLocationRepository;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public MoveShowCommandHandler(IMoveProductRepository moveProductRepository, IMoveTheLibraryRepository moveTheLibraryRepository, IBinLocationRepository binLocationRepository, IWarehouseInfoRepository warehouseInfoRepository)
        {
            //信息表（产品）
            this.moveProductRepository = moveProductRepository;
            //移库表
            this.moveTheLibraryRepository = moveTheLibraryRepository;
            _binLocationRepository = binLocationRepository;
            _warehouseInfoRepository = warehouseInfoRepository;
        }

        public async Task<ApiResult<MovepageDTO>> Handle(MoveShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<MovepageDTO> page = new ApiResult<MovepageDTO>();
            try
            {
                MovepageDTO sto = new MovepageDTO();
                var list = from a in await moveTheLibraryRepository.GetAllAsync()
                           join b in await moveProductRepository.GetAllAsync()
                           on a.MoveTheLibraryId equals b.MoveTheLibraryId
                           join c in await _binLocationRepository.GetAllAsync() on b.BinLocationId equals c.BinLocationId
                           join d in await _warehouseInfoRepository.GetAllAsync() on c.WarehouseId equals d.WarehouseId
                           where a.IsDelete == false && b.IsDelete == false && c.IsDelete == false && d.IsDelete == false
                           select new MoveShowDTO
                           {
                              
                               MoveTheLibraryId = a.MoveTheLibraryId,
                               MoveTheLibraryNo = a.MoveTheLibraryNo,
                               MoveTheLibraryType = a.MoveTheLibraryType,
                             
                               AuditStatus = a.AuditStatus,
                               ExaminePeople = a.ExaminePeople,
                               ExamineTime = a.ExamineTime,
                               MoveProductId = b.MoveProductId,
                               ProductName = b.ProductName,
                               ProductNo = b.ProductNo,
                               ProductSpec = b.ProductSpec,
                               Batch = b.Batch,
                               OutStorageNum = b.OutStorageNum,
                               BinLocationName = c.BinLocationName,
                               NewBinLocationName = b.NewBinLocationName,
                               BinLocationId = b.BinLocationId,
                               WarehouseName= d.WarehouseName,
                               IsDelete = b.IsDelete
                           };
                if (!string.IsNullOrEmpty(request.MoveTheLibraryNo))
                {
                    list = list.Where(c => c.MoveTheLibraryNo == request.MoveTheLibraryNo).ToList();
                }
                if (request.AuditStatus>0)
                {
                    list = list.Where(c => c.AuditStatus ==(AuditStatus) request.AuditStatus).ToList();
                }
                if (!string.IsNullOrEmpty(request.MoveTheLibraryType))
                {
                    list = list.Where(x => x.MoveTheLibraryType == request.MoveTheLibraryType).ToList();
                }
                sto.TotalCount = list.Count();
                sto.TotalPage = (int)Math.Ceiling(sto.TotalCount * 1.0 / request.PageSize);
                sto.List = list.OrderByDescending(c => c.MoveTheLibraryId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                page.Data = sto;
                //MovepageDTO sto=new MovepageDTO();
                //var list = from a in await moveTheLibraryRepository.GetAllAsync()
                //            join b in await moveProductRepository.GetAllAsync()
                //            on a.MoveTheLibraryId equals b.MoveTheLibraryId
                //            where a.IsDelete == false && b.IsDelete == false
                //            select new MoveShowDTO
                //            {
                //                MoveTheLibraryId = a.MoveTheLibraryId,
                //                MoveTheLibraryNo = a.MoveTheLibraryNo,
                //                MoveTheLibraryType = a.MoveTheLibraryType,
                //                AuditStatus = a.AuditStatus,
                //                ExaminePeople = a.ExaminePeople,
                //                ExamineTime = a.ExamineTime,
                //                MoveProductId = b.MoveProductId,
                //                ProductName = b.ProductName,
                //                ProductNo = b.ProductNo,
                //                ProductSpec = b.ProductSpec,
                //                Batch = b.Batch,
                //                OutStorageNum = b.OutStorageNum,
                //                BinLocationName = b.BinLocationName,
                //                BinLocationId = b.BinLocationId,
                //                IsDelete = a.IsDelete
                //            };
                //if (!string.IsNullOrEmpty(request.MoveTheLibraryNo))
                //{
                //    list = list.Where(c => c.MoveTheLibraryNo == request.MoveTheLibraryNo).ToList();
                //}
                //if (request.AuditStatus == AuditStatus.全部)
                //{
                //    list = list.Where(c => c.AuditStatus == request.AuditStatus).ToList();
                //}
                //if (!string.IsNullOrEmpty(request.MoveTheLibraryType))
                //{
                //    list = list.Where(x => x.MoveTheLibraryType == request.MoveTheLibraryType).ToList();
                //}
                //sto.TotalCount = list.Count();
                //sto.TotalPage = (int)Math.Ceiling(sto.TotalCount * 1.0 / request.PageSize);
                //sto.List = list.OrderByDescending(c => c.MoveTheLibraryId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                //page.Data = sto;
            }
            catch (Exception ex)
            {
                page.Code = -1;
                page.Message = ex.Message;
                throw;
            }
            return page;
        }
    }
}
